package com.mcptt.config.body.info.mcdata;

import android.util.Xml;

import com.cloudptt.api.product.log.Log;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.SessionInfo;

import org.xmlpull.v1.XmlPullParser;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;

public class McdataInfoBody {

	private static final String End_Mcptt_Tag = "</mcdatainfo>";

	private static final String Name_CallType = "request-type";
	private static final String Name_RequestUri = "mcdata-request-uri";
	private static final String Name_CallingUserId = "mcdata-calling-user-id";
	private static final String Name_CallingGroupId = "mcdata-calling-group-id";
	private static final String Name_Url = "mcdataURI";
	private static final String Name_ControllerPsi = "mcdata-controller-psi";

	private int type = 0;
	private String sessionType = "";
	private String peerMcdataId = "";
	private String sendMcdataId = "";
	protected String callingId = "";
	protected String requestId = "";
	private String clientId = "";
	private String controllerPsi = "";

	public McdataInfoBody()
	{
	}
	public McdataInfoBody(int type,String seeesionType,String peerMcdataId,String sendMcdataId,String clientId)
	{
		this.type = type;
		this.sessionType = seeesionType;
		this.peerMcdataId = peerMcdataId;
		this.sendMcdataId = sendMcdataId;
		this.clientId = clientId;
	}
	public McdataInfoBody(String seeesionType)
	{
		this.sessionType = seeesionType;
	}

	public String getSessionType() {
		return sessionType;
	}

	public String getSendMcdataId() {
		return sendMcdataId;
	}

	public String getPeerMcdataId() {
		return peerMcdataId;
	}



	public static McdataInfoBody parseXmlStr(String xmlStr)
	{
		McdataInfoBody body = new McdataInfoBody();
		int valType = 0;
//		String key = End_Mcptt_Tag;

		int index = xmlStr.indexOf(End_Mcptt_Tag);
		Log.debug(McdataInfoBody.class, "parseXmlStr index = " + index);

//	    if(index <= 0)
//	    {
//	    	key = End_Mcvideo_Tag;
//	    	index = xmlStr.indexOf(key);
//	    }
		xmlStr= xmlStr.substring(0, index + End_Mcptt_Tag.length());
		xmlStr = xmlStr.trim();

		try {
			ByteArrayInputStream strIs = new ByteArrayInputStream(xmlStr.getBytes());
			XmlPullParser pullParser = Xml.newPullParser();
			pullParser.setInput(strIs, "UTF-8"); //为Pull解释器设置要解析的XML数据
			int event = pullParser.getEventType();

			while (event != XmlPullParser.END_DOCUMENT) {

				switch (event) {

					case XmlPullParser.START_DOCUMENT:
						break;
					case XmlPullParser.START_TAG:
						if (Name_CallType.equals(pullParser.getName())) {
							body.sessionType = pullParser.nextText();
						} else if (Name_RequestUri.equals(pullParser.getName())) {
							valType = 1;
						} else if (Name_CallingUserId.equals(pullParser.getName())) {
							valType = 2;
						} else if (Name_CallingGroupId.equals(pullParser.getName())) {
							valType = 3;
						} else if (Name_ControllerPsi.equals(pullParser.getName())) {
							valType = 31;
						} else if (Name_Url.equals(pullParser.getName())) {
							String value = pullParser.nextText();
							Log.debug(McdataInfoBody.class, "parseXmlStr valType = " + valType + " value = " + value);
							if (valType == 1) {

							} else if (valType == 2) {
								body.sendMcdataId = value;
								if (SessionInfo.isOtoMsg(body.sessionType)) {
									body.peerMcdataId = value;
								}
							} else if (valType == 3) {
								body.peerMcdataId = value;
							} else if (valType == 31) {
								body.controllerPsi = value;
							}
							valType = 0;
						}
						break;

					case XmlPullParser.END_TAG:
						break;

				}

				event = pullParser.next();
			}
		}catch (Exception e)
		{
			e.printStackTrace();
		}

		return body;
	}

	public String toValString()
	{
		StringBuffer strBuf = new StringBuffer("McdataInfoBody ");
		strBuf.append("sessionType = ").append(sessionType).append(",");
		strBuf.append("peerMcdataId = ").append(peerMcdataId).append(",");
		strBuf.append("sendMcdataId = ").append(sendMcdataId).append(",");
		strBuf.append("callingId = ").append(callingId).append(",");
		strBuf.append("controllerPsi = ").append(controllerPsi).append(",");
		strBuf.append("clientId = ").append(clientId).append(".");

		return strBuf.toString();
	}

	public String getControllerPsi() {
		return controllerPsi;
	}

	public static String toMcdataInfoBody_Notify(String controllerPsi,String groupId)
	{
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("\r\n");
		strBuff.append("<mcdatainfo xmlns=\"urn:3gpp:ns:mcdataInfo:1.0\">").append("\r\n");
		strBuff.append("<mcdata-Params>").append("\r\n");

		strBuff.append("<mcdata-controller-psi type=\"Normal\">").append("\r\n");
		strBuff.append("<mcdataURI>").append(controllerPsi).append("</mcdataURI>").append("\r\n");
		strBuff.append("</mcdata-controller-psi>").append("\r\n");
		if(groupId != null
			&& groupId.length() > 0)
		{
			strBuff.append("<mcdata-calling-group-id type=\"Normal\">").append("\r\n");
			strBuff.append("<mcdataURI>").append(groupId).append("</mcdataURI>").append("\r\n");
			strBuff.append("</mcdata-calling-group-id>").append("\r\n");
		}

		strBuff.append("</mcdata-Params>").append("\r\n");
		strBuff.append("</mcdatainfo>");

		try {
			return new String(strBuff.toString().getBytes(),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return strBuff.toString();
	}

	public String toString()
	{
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("\r\n");
		strBuff.append("<mcdatainfo xmlns=\"urn:3gpp:ns:mcdataInfo:1.0\">").append("\r\n");
		strBuff.append("<mcdata-Params>").append("\r\n");
		if(sessionType!= null
				&& !sessionType.trim().equals("")) {
			strBuff.append("<request-type>").append(sessionType).append("</request-type>").append("\r\n");
		}

		if(peerMcdataId!= null
				&& !peerMcdataId.trim().equals("")
				&& !SessionInfo.MsgType_Desc_Notify.equals(sessionType)) {
			strBuff.append("<mcdata-request-uri>").append("\r\n");
			strBuff.append("<mcdataURI>").append(peerMcdataId).append("</mcdataURI>").append("\r\n");
			strBuff.append("</mcdata-request-uri>").append("\r\n");
		}
		if(SessionInfo.Type_Func == type)
		{
			strBuff.append("<functional-alias-URI>").append("\r\n");
//			String num = AddrInfo.toNumFromSipUrl(sendMcdataId);
			strBuff.append("<mcdataURI>").append(sendMcdataId).append("</mcdataURI>").append("\r\n");
			strBuff.append("</functional-alias-URI>").append("\r\n");
		}

//		if(sendMcdataId!= null
//				&& !sendMcdataId.trim().equals("")
//				&& !SessionInfo.MsgType_Desc_Notify.equals(sessionType)) {
//			strBuff.append("<mcdata-calling-user-id>").append("\r\n");
//			strBuff.append("<mcdataURI>").append(sendMcdataId).append("</mcdataURI>").append("\r\n");
//			strBuff.append("</mcdata-calling-user-id>").append("\r\n");
//		}

		if(clientId!= null
				&& !clientId.trim().equals("")
				&& !SessionInfo.MsgType_Desc_Notify.equals(sessionType)) {
			strBuff.append("<mcdata-client-id>").append("\r\n");
			strBuff.append("<mcdataURI>").append(clientId).append("</mcdataURI>").append("\r\n");
			strBuff.append("</mcdata-client-id>").append("\r\n");
		}

		strBuff.append("</mcdata-Params>").append("\r\n");
		strBuff.append("</mcdatainfo>");



		try {
			return new String(strBuff.toString().getBytes(),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return strBuff.toString();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
